package com.eportal.service.impl;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eportal.base.BaseDAO;
import com.eportal.domain.Sysopt;
import com.eportal.domain.TCommunity;
import com.eportal.service.AppStoreService;
import com.eportal.service.InputComuService;
import com.eportal.service.SysOptService;
import com.eportal.service.SysRolesTemplateService;
import com.eportal.service.TAgentService;
import com.eportal.service.TCommunityService;
import com.eportal.service.TLocksService;
import com.eportal.util.MD5;

/**
 * @author chencheng
 */
@Service("InputComuService")
public class InputComuServiceImpl implements InputComuService {
	private Map<String, Object> result = new HashMap<String, Object>();
	private static Logger log = Logger.getLogger(TCommunityServiceImpl.class.getName());
	@Resource(name="dao")
	private BaseDAO dao;
	@Autowired
	private TCommunityService tCommunityService;
	@Autowired
	private SysRolesTemplateService sysRolesTemplateService;
	@Autowired
	private SysOptService sysOptService;
	
	public HttpSession getSession() {
        return ServletActionContext.getRequest().getSession();
    }
	
	
	public  List<Map<String, Object>> getBySql(String sql) {
		List<Map<String, Object>> list= dao.queryBySql(sql);
		return list;
	}
	
	public Map<String, Object> addtCommunity(TCommunity tCommunity, String roleId, String status) {
		try {
			result = new HashMap<String, Object>();
			long idNext  = dao.findSequence("SEQ_T03_COMMUNITY");
			Map<String, Object> resultSysopt = creatComuManager(tCommunity,idNext+"",roleId);
			if (!"1".equals(resultSysopt.get("code").toString())) {
				result.put("code", 0);
				result.put("msg", resultSysopt.get("msg"));
				return result;
			}
			tCommunity.setSid(idNext);
			tCommunity.setAdminUser(resultSysopt.get("sid").toString());
			tCommunity.setComm("");
			tCommunity.setcUserName("1");
			tCommunity.setaUserName("1");
			tCommunity.setcTime(new Timestamp(System.currentTimeMillis()));
			tCommunity.setStatus(status);
			tCommunity.setOperateComm("妙兜迁移操作");
			tCommunity.setaTime(new Timestamp(System.currentTimeMillis()));
			 
			tCommunity = tCommunityService.add(tCommunity);
			
			//创建社区超级管理员角色
			 result = sysRolesTemplateService.creatComuManager(tCommunity);
			 if ("0".equals(result.get("code").toString())) {
				 return result;
			 }
			 //更新超级管理员角色权限
			 sysOptService.updateManager(tCommunity.getAdminUser(),result.get("roleId").toString());
			result.put("objectId", tCommunity.getSid());
			result.put("tCommunity", tCommunity);
			result.put("code", 1);
			result.put("msg", "操作成功");
		} catch (Exception e) {
			log.error("社区保存失败", e);
			result.put("code", 0);
			result.put("msg", "操作失败");
		}
		return result;
	}
	
	
	public Map<String, Object> creatComuManager(TCommunity tCommunity, String communityId, String roleId) {
		 try {
	          	Sysopt sysopt = new Sysopt();
	          	sysopt.setUserName(tCommunity.getAdminUser());
	          	sysopt.setPasswd(tCommunity.getComm());
	          	if(sysOptService.ifUserExist(sysopt)){
	          		result.put("code", 0);
	                  result.put("msg", "账号已存在");
	                  return result;
	          	}
	          	sysopt.setCommunityId(communityId);
//	           	if("3".equals(roleId)){
	           		sysopt.setrLevel(2);
//	           	}else{
//	           		sysopt.setrLevel(4);
//	           	}
	          	sysopt.setAgtCode(tCommunity.getAgtCode());
	          	sysopt.setIsAdmin("1");
	          	sysopt.setcTime(new Timestamp(System.currentTimeMillis()));
	          	sysopt.setPasswd(sysopt.getPasswd());
	        	sysopt.setTel(tCommunity.getDepartTel());
	          	sysopt.setAddr(tCommunity.getStreet());
	          	sysopt.setUname(tCommunity.getComContact());
	          	sysopt.setStatus("1");
	          	sysopt.setRoles(roleId);//未审核通过的角色
	          	Sysopt sys= sysOptService.save(sysopt);
	          	result.put("sid", sys.getSid());
	          	result.put("code", 1);
	            result.put("msg", "操作员保存成功");
	          } catch (Exception e) {
	              log.error("添加社区管理员失败", e);
	              result.put("code", 0);
	              result.put("msg", "操作员生成失败");
	          }
	          return result;
	}
}
